package com.fbt.transfer.app.driver.baidu;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.*;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.baidu.navisdk.adapter.*;
import com.baidu.navisdk.adapter.struct.*;
import com.fbt.transfer.app.driver.R;
import com.fbt.transfer.app.driver.model.index.OrderUnitVo;
import com.fbt.transfer.app.driver.view.SlideToView;

public class NaviGuideFragment extends Fragment {

  private OnNavigationCompleteListener navigationCompleteListener;
  private SlideCompleteListener slideCompleteListener;

  public NaviGuideFragment(
      SlideCompleteListener slideCompleteListener,
      OnNavigationCompleteListener navigationCompleteListener) {
    this.slideCompleteListener = slideCompleteListener;
    this.navigationCompleteListener = navigationCompleteListener;
  }

  // private OnNavigationCompleteListener navigationCompleteListener; // Removed this duplicate

  private void initListener() {
    BaiduNaviManagerFactory.getRouteGuideManager()
        .setNaviListener(
            new IBNaviListener() {
              @Override
              public void onYawingSuccess() {
                BNRoutePlanInfos routePlanInfo =
                    BaiduNaviManagerFactory.getRoutePlanManager().getRoutePlanInfo();
              }

              @Override
              public void onNaviGuideEnd() {
                FragmentActivity activity = getActivity();
                if (activity != null) {
                  activity.getSupportFragmentManager().popBackStack();
                } else {
                  Log.e("NaviGuideFragment", "Activity is null when navigation guide ended");
                }
                // Notify the listener instead of finishing the activity
                if (navigationCompleteListener != null) {
                  navigationCompleteListener.onNavigationComplete();
                }
              }
            });
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  public interface SlideCompleteListener {
    void OnSlideCompleteListener(SlideToView slideToNavigate);
  }
  private OrderUnitVo orderInfo;
 private Context context;
  @Nullable
  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater,
      @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    orderInfo = getArguments().getSerializable("orderInfo", OrderUnitVo.class);
    RelativeLayout mRootView =
        (RelativeLayout) inflater.inflate(R.layout.fragment_navi_guide, container, false);
    Bundle bundle = new Bundle();
    bundle.putBoolean(BNaviCommonParams.ProGuideKey.ADD_MAP, true);
    bundle.putBoolean(BNaviCommonParams.ProGuideKey.IS_REALNAVI, true);
    bundle.putBoolean(BNaviCommonParams.ProGuideKey.IS_SUPPORT_FULL_SCREEN, true);
    context = getActivity();
    int finalCustomViewHeightBottom = 200;
    BNGuideConfig config =
        new BNGuideConfig.Builder()
            // 导航工具栏下方view
            .addBelowBottomView(
                new IBNRouteGuideManager.NaviAddViewCallback() {

                  @Override
                  public int getViewHeight() {
                    return finalCustomViewHeightBottom;
                  }

                  @Override
                  public View getAddedView() {

                    // 创建一个SlideToNavigateView
                    SlideToView slideToNavigate = new SlideToView(context);
                    slideToNavigate.setId(View.generateViewId()); // 生成唯一 ID

                    // 设置 LayoutParams（match_parent width, 60dp height）
                    RelativeLayout.LayoutParams layoutParams =
                        new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.MATCH_PARENT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    layoutParams.setMargins(
                        (int)
                            TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP,
                                16,
                                getResources().getDisplayMetrics()),
                        0,
                        (int)
                            TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP,
                                16,
                                getResources().getDisplayMetrics()),
                        (int)
                            TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP,
                                16,
                                getResources().getDisplayMetrics()));
                    slideToNavigate.setLayoutParams(layoutParams);
                    // 可选：设置滑动完成监听器
                    slideToNavigate.setOnSlideCompleteListener(
                        () -> slideCompleteListener.OnSlideCompleteListener(slideToNavigate));
                    return slideToNavigate;
                  }
                })
            .params(bundle)
            .build();
    container.addView(
        BaiduNaviManagerFactory.getRouteGuideManager().onCreate(getActivity(), config));
    initListener();
    return mRootView;
  }

  // Define a new interface for navigation completion
  public interface OnNavigationCompleteListener {
    void onNavigationComplete();
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    BaiduNaviManagerFactory.getRouteGuideManager().onConfigurationChanged(newConfig);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    BaiduNaviManagerFactory.getRouteGuideManager().onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public void onStart() {
    super.onStart();
    BaiduNaviManagerFactory.getRouteGuideManager().onStart();
  }

  @Override
  public void onResume() {
    super.onResume();
    BaiduNaviManagerFactory.getRouteGuideManager().onResume();
  }

  @Override
  public void onPause() {
    super.onPause();
    BaiduNaviManagerFactory.getRouteGuideManager().onPause();
  }

  @Override
  public void onStop() {
    super.onStop();
    BaiduNaviManagerFactory.getRouteGuideManager().onStop();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    BaiduNaviManagerFactory.getRouteGuideManager().onDestroy(false);
  }
}
